package com.settlement.system.service.impl;

import com.settlement.system.common.util.StringUtils;
import com.settlement.system.model.entity.JdOrder;
import com.settlement.system.mapper.JdOrderMapper;
import com.settlement.system.model.entity.SysStore;
import com.settlement.system.service.JdOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.service.SysStoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.JdOrderQuery;
import com.settlement.system.model.form.JdOrderForm;
import cn.hutool.core.bean.BeanUtil;
import com.settlement.system.model.vo.JdOrderImportVo;

/**
 * <p>
 * 京东订单 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-04-13
 */
@Service
@RequiredArgsConstructor
public class JdOrderServiceImpl extends ServiceImpl<JdOrderMapper, JdOrder> implements JdOrderService {

    private final SysStoreService sysStoreService;

    @Override
    public Page<JdOrder> getPage(JdOrderQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        String tableName = "jd_order_%s".formatted(queryParams.getBillMonth());
        queryParams.setTableName(tableName);
        if (!StringUtils.hasText(this.baseMapper.existTable(tableName))) {
            return new Page<>(pageNum, pageSize);
        }
        Page<JdOrder> page = new Page<>(pageNum, pageSize);
        this.baseMapper.getPage(page, queryParams);
        SysStore sysStore;
        for (JdOrder record : page.getRecords()) {
            sysStore = sysStoreService.getById(record.getStoreId());
            if (sysStore != null) {
                record.setStoreName(sysStore.getBillSystemStoreName());
            }
        }
        return page;
    }

     @Override
     public boolean saveJdOrder(JdOrderForm jdOrderForm) {
         JdOrder jdOrder = new JdOrder();
         BeanUtil.copyProperties(jdOrderForm, jdOrder);
         jdOrder.setCreateTime(null);
         return this.saveOrUpdate(jdOrder);
     }

     @Override
     public JdOrderForm getJdOrderFormData(Long id) {
         JdOrder jdOrder = this.getById(id);
         JdOrderForm jdOrderForm = new JdOrderForm();
         BeanUtil.copyProperties(jdOrder, jdOrderForm);
         return jdOrderForm;
     }

     @Override
     public boolean deleteJdOrder(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<JdOrderImportVo> voList)  {
        List<JdOrder> jdOrderList = new ArrayList<>();
        for (JdOrderImportVo importVo : voList) {
            JdOrder jdOrder = new JdOrder();
            BeanUtil.copyProperties(importVo, jdOrder);
            jdOrderList.add(jdOrder);
        }
        this.saveOrUpdateBatch(jdOrderList);
    }

    @Override
    public List<JdOrderImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<JdOrder> jdOrderList = this.getBaseMapper().selectBatchIds(ids);
        List<JdOrderImportVo> voList = new ArrayList<>();
        for (JdOrder jdOrder : jdOrderList) {
            JdOrderImportVo vo = new JdOrderImportVo();
            BeanUtil.copyProperties(jdOrder, vo);
            voList.add(vo);
        }
        return voList;
    }
}
